package org.dong.servlet;

import com.alibaba.fastjson.JSON;
import org.dong.Tool.ApplicationUtil;
import org.dong.pojo.Dto;
import org.dong.pojo.User;
import org.dong.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getUserList")
public class MangeUserListServlet extends HttpServlet {

    @Autowired
    UserService userService;
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dto dto = new Dto();

        int page = Integer.parseInt(req.getParameter("page"));

        UserService userServiceImpl = (UserService) ApplicationUtil.getContext().getBean("userServiceImpl");

        List<User> userList = userServiceImpl.getUserList(page);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list",userList);

        dto.setDatas(data);
        dto.setStatusCode(200);
        dto.setErrMsg("会员列表");


        String msg = JSON.toJSONString(dto);

        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().println(msg);
    }
}
